//
//  LYMineOrzViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/8.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYMineOrzViewController.h"
#import "LYOrzCell.h"
#import "LYSearchBar.h"

@interface LYMineOrzViewController ()

@property (weak, nonatomic) IBOutlet UILabel *currentOrz;

@property (nonatomic, strong)NSMutableArray *models;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) UIBarButtonItem *searchItem;

@property (nonatomic, strong) UIBarButtonItem *cancelItem;

@property (nonatomic, strong) LYSearchBar *searchBar;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isSearching;


@end

@implementation LYMineOrzViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpNav];
    
    [self getCurrentOrz];
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 初始化
/**
 *  初始化导航栏
 */
- (void)setUpNav{
    
    self.navigationItem.rightBarButtonItem = self.searchItem;
    
}


#pragma mark - 懒加载

- (UIBarButtonItem *)searchItem{
    
    if (_searchItem == nil) {
        _searchItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"搜索-blue"] style:UIBarButtonItemStyleDone target:self action:@selector(search)];
    }
    return _searchItem;
}

- (UIBarButtonItem *)cancelItem{
    
    if (_cancelItem == nil) {
        _cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(search)];
    }
    return _cancelItem;
}

- (LYSearchBar *)searchBar{
    
    if (_searchBar == nil) {
        LYSearchBar *searchBar = [[LYSearchBar alloc] init];
        searchBar.frame = CGRectMake(0, 0, CGFLOAT_MAX, 30);
        searchBar.placeholder = @"搜索科目、老师、课程、机构";
        [searchBar addTarget:self action:@selector(textfieldChange:) forControlEvents:UIControlEventEditingChanged];
        
        _searchBar = searchBar;
    }
    return _searchBar;
}


#pragma mark - 网络请求

/**
 *  请求新数据
 */
- (void)loadNewData{
        
    NSString *keyword = self.isSearching ? self.searchBar.text : nil;
    
    [LYRequestTool searchWithToken:LYToken keyword:keyword role_id:LYRoleTypeOrganization page:@"1" per_page:@"10" success:^(NSArray<LYPersonalInfo *> *models) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        self.models = [models mutableCopy];
        [self.tableView reloadData];
        self.page = 1;
        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_header endRefreshing];
        [MBProgressHUD showOnlyText:message];
    }];
    
}
/**
 *  加载更多数据
 */
- (void)loadMoreData{
    
    
    
    NSString *page = [NSString stringWithFormat:@"%zd",self.page + 1];
    NSString *perpage = @"10";
    NSString *keyword = self.isSearching ? self.searchBar.text : nil;
    
    
    [LYRequestTool searchWithToken:LYToken keyword:keyword role_id:LYRoleTypeOrganization page:page per_page:perpage success:^(NSArray<LYPersonalInfo *> *models) {
        
        if (models.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.models addObjectsFromArray:models];;
        self.page++;
        [self.tableView reloadData];
        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_header endRefreshing];
        [MBProgressHUD showOnlyText:message];
    }];

    
    
}


/**
 *  添加机构
 */
- (void)addOrzWithOrzID:(NSString *)ID{


    [LYRequestTool addOriganzationWithToken:LYToken to_user_id:ID success:^{
       
        [MBProgressHUD showOnlyText:@"添加成功，等待机构确认"];
        
    } failure:^(NSString *code, NSString *message) {
        
        [MBProgressHUD showOnlyText:message];
        
    }];

}


/**
 *  当前机构
 */
- (void)getCurrentOrz{


    [LYRequestTool getMyOrganizationWithToken:LYToken success:^(LYMineOrzInfoModel * model) {
        
        self.currentOrz.text = [NSString stringWithFormat:@"已经加入：%@",model.nick_name];
        
    } failure:^(NSString *code, NSString *message) {
        self.currentOrz.text = @"暂未加入机构";
    }];

}

#pragma mark - 上下拉刷新
/**
 *  下拉
 */
- (void)tableViewHeaderRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadNewData];
    
}

/**
 *  上拉
 */
- (void)tableViewfooterRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadMoreData];
    
}

#pragma mark - textFiled输入监听

- (void)textfieldChange:(UITextField *)textField{
    
    [self loadNewData];
    
}


#pragma mark - tableviewDataSource


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.models.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"LYTeacherCell";
    
    LYOrzCell *cell = [LYOrzCell cellWithTableView:tableView andIndetifier:cellID];
    
    LYPersonalInfo *model = self.models[indexPath.section];
    model.showCancelFollowBtn = NO;
    cell.model = model;
    return cell;
}

#pragma mark - tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0.1;
    }else{
        return 5;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYPersonalInfo *model = self.models[indexPath.section];
    //添加机构
    [self addOrzWithOrzID:model.user_id];
    
}

#pragma mark - 按钮点击
- (void)search{
    
    //是否正在搜索
    if (self.isSearching) {//是(取消搜索操作)
        
        //修改rightItem
        self.navigationItem.rightBarButtonItem = self.searchItem;
        
        //修改titleView
        self.navigationItem.titleView = nil;
        
        //清除搜索关键词
        self.searchBar.text = @"";
        
        
        //重新刷新列表
        [self loadNewData];
        
        
    }else{//否(进入搜索)
        
        //修改rightItem
        self.navigationItem.rightBarButtonItem = self.cancelItem;
        
        //修改titleView
        self.navigationItem.titleView = self.searchBar;
        
        
        //弹出键盘
        [self.searchBar becomeFirstResponder];
        
    }
    
    
    //改变状态
    self.isSearching = !self.isSearching;
    
    
}



@end
